package cn.mall.goods.controller;

import cn.mall.auth.annotation.MyPermission;
import cn.mall.base.utils.FastdfsUtil;
import cn.mall.base.utils.LoginContext;
import cn.mall.goods.dto.ExamineDTO;
import cn.mall.goods.service.IGoodsService;
import cn.mall.goods.domain.Goods;
import cn.mall.goods.query.GoodsQuery;
import cn.mall.base.utils.PageList;
import cn.mall.base.utils.AjaxResult;
import cn.mall.order.domain.PicVo;
import cn.mall.order.utils.Utils;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/goods")
@MyPermission(name = "商品管理")
public class GoodsController {

    @Autowired
    public IGoodsService goodsService;

    @Autowired
    private LoginContext loginContext;



    private static final String UPLOADED_FOLDER = "/www/wwwroot/wb_mall/dist/pic/";
//    private static final String UPLOADED_FOLDER = "D:\\weibo_mall\\pic\\";

    @PostMapping("/upload") // 与前端POST请求的URL匹配
    public ResponseEntity<?> handleFileUpload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("请选择一个文件上传");
        }

        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);

            return ResponseEntity.ok(UPLOADED_FOLDER + file.getOriginalFilename());
        } catch (Exception e) {
            return ResponseEntity.status(500).body("文件上传失败: " + e.getMessage());
        }
    }

    @PostMapping("/remove")
    public  AjaxResult delete(@RequestBody String path){
        try {
            System.out.println(path);

            File file = new File(path);
            if (file.exists()) {
                if (file.delete()) {
                    System.out.println("文件已成功删除");
                } else {
                    System.out.println("文件删除失败");
                }
            } else {
                System.out.println("文件不存在");
            }
            return AjaxResult.me();

        } catch (Exception e) {
            e.printStackTrace(); // 通常不应在生产代码中打印堆栈跟踪
            return AjaxResult.me().setSuccess(false).setMessage("删除异常: " + e.getMessage());
        }
    }




    /**
     * 保存和修改公用的
     *
     * @param goods 传递的实体
     * @return Ajaxresult转换结果
     */
    @MyPermission(name = "商品的添加/修改", desc = "商品的添加/修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Goods goods) {
        try {
            if (goods.getId() != null)
                goodsService.update(goods);
            else
                goodsService.insert(goods);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @MyPermission(name = "商品删除",desc = "商品删除")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            goodsService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @MyPermission(name = "商品批量删除", desc = "商品批量删除")
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            goodsService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！" + e.getMessage());
        }
    }

    /**
     * 根据Id获取用户
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult loadById(@PathVariable("id") Long id) {
        try {
            Goods goods = goodsService.loadById(id);
            return AjaxResult.me().setResultObj(goods);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */

    @GetMapping
    public AjaxResult loadAll() {

        try {
            List<Goods> list = goodsService.loadAll();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/page")
    public AjaxResult pageList(@RequestBody GoodsQuery query) {
        try {
            PageList<Goods> pageList = goodsService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/pageV2")
    public AjaxResult pageListV2(@RequestBody GoodsQuery query) {
        try {
            PageList<Goods> pageList = goodsService.pageListV2(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }





    @PostMapping("/newpage")
    public AjaxResult newpageList(@RequestBody GoodsQuery query) {
        try {
            PageList<Goods> pageList = goodsService.newpageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
     * 上架
     * @param ids
     * @return
     */
    @MyPermission(name = "商品上架",desc = "商品上架")
    @PostMapping("/onSale")
    public AjaxResult onSale(@RequestBody List<Long> ids) {
        try {
            String msg = goodsService.onSale(ids); //0--上架  1--下架
            return AjaxResult.me().setMessage(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量上架失败！" + e.getMessage());
        }
    }

    /**
     * 下架
     *
     * @param ids
     * @return
     */
    @MyPermission(name = "商品下架", desc = "商品下架")
    @PostMapping("/offSale")
    public AjaxResult offSale(@RequestBody List<Long> ids) {
        try {
            String msg = goodsService.offSale(ids);//0--上架  1--下架
            return AjaxResult.me().setMessage(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量下架失败！" + e.getMessage());
        }
    }

    /**
     * 手动审核提交
     * @param examineDTO
     * @return
     */
    @PostMapping("/audit")
    @MyPermission(name = "商品审核",desc = "商品审核")
    public AjaxResult manualAudit(@RequestBody ExamineDTO examineDTO) {
        try {
            goodsService.manualAudit(examineDTO);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核失败！" + e.getMessage());
        }
    }




}
